﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Xajhzwb.Components.Data;
using Xajhzwb.Components.Util;
using System.IO;
using Xajhzwb.Components.Helper;

namespace Xajhzwb
{
    public partial class FormGlobal : Form
    {
        public FormGlobal()
        {
            InitializeComponent();
        }

        /// <summary>
        /// form load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormKey_Load(object sender, EventArgs e)
        {
            try
            {
                // load tree
                loadTreeView();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// confirm button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            btnApply_Click(sender, e);
            this.Close();
        }

        /// <summary>
        /// canncel button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// apply button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnApply_Click(object sender, EventArgs e)
        {
            try
            {
                GlobalData globalData = new GlobalData();
                globalData.ID = int.Parse(treeView.SelectedNode.Name);

                globalData.KeyTitle = txtKeyTitle.Text;
                globalData.KeyContent = txtKeyContent.Text;
                globalData.MgTitle = txtMgTitle.Text;
                globalData.MgContent = txtMgContent.Text;
                globalData.Rewriter = txtRewriter.Text;
                globalData.IsImgMark = chkIsImgMark.Checked;
                globalData.ImgMarkFile = txtImgMarkFile.Text;

                if (rboImgWz1.Checked) globalData.ImgWz = 1;
                if (rboImgWz2.Checked) globalData.ImgWz = 2;
                if (rboImgWz3.Checked) globalData.ImgWz = 3;
                if (rboImgWz4.Checked) globalData.ImgWz = 4;
                if (rboImgWz5.Checked) globalData.ImgWz = 5;
                if (rboImgWz6.Checked) globalData.ImgWz = 6;
                if (rboImgWz7.Checked) globalData.ImgWz = 7;
                if (rboImgWz8.Checked) globalData.ImgWz = 8;
                if (rboImgWz9.Checked) globalData.ImgWz = 9;

                globalData.ImgAlpha = trackBarAphla.Value;

                int result = DataHelper.UpdGlobalData(globalData);
                btnApply.Enabled = false;

                Config config = new Config();
                config.RecGlobal = treeView.SelectedNode.Name.ToString();
            }
            catch (Exception ex)
            {
                Utils.ShowMessageBox(ex.Message, "エラー情報", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void loadTreeView()
        {
            treeView.Nodes.Clear();

            List<ClassData> classDataList = DataHelper.GetClassNameList(Const.ClassType.Global.ToString());
            if (classDataList != null && classDataList.Count > 0)
            {
                for (int i = 0; i < classDataList.Count; i++)
                {
                    TreeNode classNode = new TreeNode();
                    classNode.Name = "C" + classDataList[i].ClassID.ToString();
                    classNode.Text = classDataList[i].ClassName.ToString();
                    classNode.Tag = classDataList[i];

                    List<GlobalData> globalDataList = DataHelper.GetGlobalDataList(classDataList[i].ClassID);
                    if (globalDataList != null && globalDataList.Count > 0)
                    {
                        foreach (GlobalData globalData in globalDataList)
                        {
                            TreeNode globalNode = new TreeNode();
                            globalNode.Name = globalData.ID.ToString();
                            globalNode.Text = globalData.Name.ToString();
                            globalNode.Tag = globalData;

                            classNode.Nodes.Add(globalNode);
                        }
                    }

                    treeView.Nodes.Add(classNode);
                }
            }

            //if(treeView != null && treeView.Nodes.Count > 0)
            //{
            //    Config config = new Config();
            //    treeView.SelectedNode.Name = config.RecGlobal;
            //}
        }

        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView.SelectedNode.Name.Substring(0, 1) != "C")
            {
                loadGlobalData(treeView.SelectedNode.Tag as GlobalData);
            }
        }

        private void btnImgMark_Click(object sender, EventArgs e)
        {
            openFileDialog.InitialDirectory = Utils.StartupPath;
            openFileDialog.Filter = "All files (*.*)|*.*|gif files (*.gif)|*.gif";
            openFileDialog.FilterIndex = 2;
            openFileDialog.RestoreDirectory = true;

            if(openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtImgMarkFile.Text = openFileDialog.FileName;
            }
        }

        private void loadGlobalData(GlobalData globalData)
        {
            if (globalData != null)
            {
                lblName.Text = globalData.Name;
                txtKeyTitle.Text = globalData.KeyTitle;
                txtKeyContent.Text = globalData.KeyContent;
                txtMgTitle.Text = globalData.MgTitle;
                txtMgContent.Text = globalData.MgContent;
                txtRewriter.Text = globalData.Rewriter;
                chkIsImgMark.Checked = globalData.IsImgMark;
                txtImgMarkFile.Text = globalData.ImgMarkFile;

                rboImgWz1.Checked = (globalData.ImgWz == 1);
                rboImgWz2.Checked = (globalData.ImgWz == 2);
                rboImgWz3.Checked = (globalData.ImgWz == 3);
                rboImgWz4.Checked = (globalData.ImgWz == 4);
                rboImgWz5.Checked = (globalData.ImgWz == 5);
                rboImgWz6.Checked = (globalData.ImgWz == 6);
                rboImgWz7.Checked = (globalData.ImgWz == 7);
                rboImgWz8.Checked = (globalData.ImgWz == 8);
                rboImgWz9.Checked = (globalData.ImgWz == 9);

                trackBarAphla.Text = globalData.ImgAlpha.ToString();
                //txtLogoW.Text = globalData.LogoW.ToString();
                //txtLogoH.Text = globalData.LogoH.ToString();
            }
        }

        private void trackBarAphla_Scroll(object sender, EventArgs e)
        {
            preViewImgMask();
        }

        private void rboImgWz1_CheckedChanged(object sender, EventArgs e)
        {
            preViewImgMask();

        }

        private void rboImgWz2_CheckedChanged(object sender, EventArgs e)
        {
            preViewImgMask();

        }

        private void rboImgWz3_CheckedChanged(object sender, EventArgs e)
        {
            preViewImgMask();

        }

        private void rboImgWz4_CheckedChanged(object sender, EventArgs e)
        {
            preViewImgMask();

        }

        private void rboImgWz5_CheckedChanged(object sender, EventArgs e)
        {
            preViewImgMask();

        }

        private void rboImgWz6_CheckedChanged(object sender, EventArgs e)
        {
            preViewImgMask();

        }

        private void rboImgWz7_CheckedChanged(object sender, EventArgs e)
        {
            preViewImgMask();

        }

        private void rboImgWz8_CheckedChanged(object sender, EventArgs e)
        {
            preViewImgMask();

        }

        private void rboImgWz9_CheckedChanged(object sender, EventArgs e)
        {
            preViewImgMask();

        }

        private void btnRewriterImp_Click(object sender, EventArgs e)
        {
            openFileDialog.InitialDirectory = Utils.StartupPath;
            openFileDialog.Filter = "All files (*.*)|*.*|txt files (*.txt)|*.txt";
            openFileDialog.FilterIndex = 2;
            openFileDialog.RestoreDirectory = true;

            if(openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string rewriterFile = openFileDialog.FileName;

                FileStream fs = new FileStream(rewriterFile, FileMode.Open, FileAccess.Read);

                using(StreamReader m_streamReader = new StreamReader(fs))
                {
                    m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);

                    txtRewriter.Text = "";
                    string strLine = m_streamReader.ReadLine();
                    while(strLine != null)
                    {
                        this.txtRewriter.Text += strLine + "\n";
                        strLine = m_streamReader.ReadLine();
                    }
                }
            }
        }

        private void btnRewriterExp_Click(object sender, EventArgs e)
        {

        }

        private void preViewImgMask()
        {
            ImageHelper.WaterMarkPosition postion = ImageHelper.WaterMarkPosition.TopLeft;

            if (rboImgWz1.Checked) postion = ImageHelper.WaterMarkPosition.TopLeft;
            if (rboImgWz2.Checked) postion = ImageHelper.WaterMarkPosition.TopMiddle;
            if (rboImgWz3.Checked) postion = ImageHelper.WaterMarkPosition.TopRight;
            if (rboImgWz4.Checked) postion = ImageHelper.WaterMarkPosition.MiddleLeft;
            if (rboImgWz5.Checked) postion = ImageHelper.WaterMarkPosition.Center;
            if (rboImgWz6.Checked) postion = ImageHelper.WaterMarkPosition.MiddleRight;
            if (rboImgWz7.Checked) postion = ImageHelper.WaterMarkPosition.BottomLeft;
            if (rboImgWz8.Checked) postion = ImageHelper.WaterMarkPosition.BottomMiddle;
            if (rboImgWz9.Checked) postion = ImageHelper.WaterMarkPosition.BottomRight;

            if (!String.IsNullOrEmpty(txtImgMarkFile.Text))
            {
                Image markImage = Image.FromFile(txtImgMarkFile.Text);
                pbImgMark.Image = ImageHelper.DrawWaterMarkImage(pbImgMark.BackgroundImage, markImage, postion, float.Parse(trackBarAphla.Value.ToString()), Color.Transparent);
            }
        }

        private void addClassToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClassData classData = new ClassData();
            classData.ClassName = "New Floder";
            classData.ClassType = Const.ClassType.Global.ToString();
            classData.Sort = 0;
            DataHelper.InsClassData(classData);

            TreeNode newNode = new TreeNode();
            newNode.Name = "C" + DataHelper.GetMaxClassID(Const.ClassType.Global.ToString()).ToString();
            newNode.Text = classData.ClassName;

            treeView.BeginUpdate();
            treeView.Nodes.Add(newNode);
            treeView.EndUpdate();
            treeView.SelectedNode = newNode;
        }

        private void addRuleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GlobalData globalData = new GlobalData();
            globalData.Name = "New Rule";
            if (treeView.SelectedNode.Name.Substring(0, 1) != "C")
            {
                globalData.ClassID = int.Parse(treeView.SelectedNode.Parent.Name.Replace("C", ""));
            } else{
                globalData.ClassID = int.Parse(treeView.SelectedNode.Name.Replace("C", ""));
            }
            DataHelper.InsGlobalData(globalData);

            TreeNode newNode = new TreeNode();
            newNode.Name = DataHelper.GetMaxGlobalID().ToString();
            newNode.Text = globalData.Name;
            newNode.Tag = DataHelper.GetGlobalData(int.Parse(newNode.Name));

            treeView.BeginUpdate();
            treeView.Nodes[treeView.SelectedNode.Index].Nodes.Add(newNode);
            treeView.EndUpdate();
            treeView.SelectedNode = newNode;
        }

        private void renameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.treeView.SelectedNode.BeginEdit();
        }

        private void extToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.treeView.ExpandAll();
        }

        private void colToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.treeView.CollapseAll();
        }

        private void delToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.treeView.SelectedNode.Name.Substring(0, 1) != "C")
            {
                int ID = int.Parse(this.treeView.SelectedNode.Name);
                DataHelper.DelGlobalData(ID);
            }

            if (this.treeView.SelectedNode.Name.Substring(0, 1) == "C")
            {
                int classID = int.Parse(this.treeView.SelectedNode.Name.Substring(1));
                DataHelper.DelClassData(classID);    
            }

            this.treeView.Nodes.Remove(this.treeView.SelectedNode);
        }

        private void treeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            if (e.Label == "" && e.Label == null)
            {
                e.CancelEdit = true;
                return;
            }

            if (treeView.SelectedNode.Name.Substring(0, 1) != "C")
            {
                GlobalData globalData = treeView.SelectedNode.Tag as GlobalData;
                globalData.Name = e.Label;
                DataHelper.UpdGlobalData(globalData);
            }
            else
            {
                ClassData classData = treeView.SelectedNode.Tag as ClassData;
                classData.ClassName = e.Label;
                DataHelper.UpdClassData(classData);
            }
        }



    }
}
